<template>
	<el-dialog :title="form.payRecordId ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="支付单ID" prop="orderId">
						<el-input v-model="form.orderId" placeholder="请输入支付单ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付方式id" prop="methodId">
						<el-input v-model="form.methodId" placeholder="请输入支付方式id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="账户ID" prop="cardId">
						<el-input v-model="form.cardId" placeholder="请输入账户ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="通道ID" prop="wayId">
						<el-input v-model="form.wayId" placeholder="请输入通道ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付方式编码" prop="methodCd">
						<el-input v-model="form.methodCd" placeholder="请输入支付方式编码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付通道编码" prop="wayCd">
						<el-input v-model="form.wayCd" placeholder="请输入支付通道编码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付账号" prop="cardNbr">
						<el-input v-model="form.cardNbr" placeholder="请输入支付账号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="服务费率" prop="feeRate">
						<el-input v-model="form.feeRate" placeholder="请输入服务费率" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="服务费" prop="feeAmount">
						<el-input v-model="form.feeAmount" placeholder="请输入服务费" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="实付金额" prop="payAmount">
						<el-input v-model="form.payAmount" placeholder="请输入实付金额" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="通道用户标识" prop="wayUser">
						<el-input v-model="form.wayUser" placeholder="请输入通道用户标识" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="通道订单号" prop="wayOrderNo">
						<el-input v-model="form.wayOrderNo" placeholder="请输入通道订单号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="订单失效时间" prop="expiredTime">
						<el-input v-model="form.expiredTime" placeholder="请输入订单失效时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付成功时间" prop="successTime">
						<el-input v-model="form.successTime" placeholder="请输入支付成功时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="渠道支付错误码" prop="errcode">
						<el-input v-model="form.errcode" placeholder="请输入渠道支付错误码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="支付错误描述" prop="errmsg">
						<el-input v-model="form.errmsg" placeholder="请输入支付错误描述" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="扩展信息" prop="extInfo">
						<el-input v-model="form.extInfo" placeholder="请输入扩展信息" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="状态" prop="status">
						<el-input v-model="form.status" placeholder="请输入状态" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="创建时间" prop="createAt">
						<el-input v-model="form.createAt" placeholder="请输入创建时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="更新时间" prop="updateAt">
						<el-input v-model="form.updateAt" placeholder="请输入更新时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="租户" prop="tanentId">
						<el-input v-model="form.tanentId" placeholder="请输入租户" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="PayOrderRecordDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/payment/PayOrderRecord';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	payRecordId: '',
	orderId: '',
	methodId: '',
	cardId: '',
	wayId: '',
	methodCd: '',
	wayCd: '',
	cardNbr: '',
	feeRate: '',
	feeAmount: '',
	payAmount: '',
	wayUser: '',
	wayOrderNo: '',
	expiredTime: '',
	successTime: '',
	errcode: '',
	errmsg: '',
	extInfo: '',
	status: '',
	createAt: '',
	updateAt: '',
	tanentId: '',
});

// 定义校验规则
const dataRules = ref({});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.payRecordId = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取payOrderRecord信息
	if (id) {
		form.payRecordId = id;
		getPayOrderRecordData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.payRecordId ? await putObj(form) : await addObj(form);
		useMessage().success(form.payRecordId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getPayOrderRecordData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ payRecordId: id })
		.then((res: any) => {
			Object.assign(form, res.data[0]);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
